/**
* @author王华贵 ：filterNum
* @Description ：该方法是为了将数字转化一种显示的格式。例如：2000000 ==》200万，且保留两位有效小数点
* @param：需要被转化的数字
* @return：转化后的数字
*/
// 递归处理末尾的0
function handler(str: string) {
    if (str[str.length - 1] !== "." && str[str.length - 1] === "0") {
        str = str.slice(0, str.length - 1);
        str = handler(str);
    }
    return str;
}
// 万级与亿级的切换
function changeRes(numStr: string, val: string, large: number) {
    let numStrArr: string[] = numStr.split("");
    let largeNum = 4 * large;
    numStrArr.splice(numStr.length - largeNum, 0, ".");
    let str = numStrArr.join("");
    let index = str.indexOf(".");
    str = handler(str.slice(0, index + 3));
    str = str[str.length - 1] === "." ? str.split(".")[0] + val : str + val;
    return str;
}

const filterNum = (num: number) => {
    let numStr = num + "";
    if (numStr.length <= 4) {
        return numStr;
    } else if (numStr.length <= 8) {
        return changeRes(numStr, "万", 1);
    } else if (numStr.length <= 12) {
        return changeRes(numStr, "亿", 2);
    }
}

export default filterNum;